// import { login, logout, getInfo, getMenu } from '@/api/user';
// import { getToken, setToken, removeToken } from '@/utils/auth';
// import router, { resetRouter } from '@/router';
// import _ from 'lodash';

const state = {
    token: '',
    userName: '',
    avatar: '',
    introduction: '',
    roles: [],
    menu: [],
    userinfo: '',
    isLogin: 0,
    isClose: 0,
    menuData: window.localStorage.getItem('menuData') !== [] ? JSON.parse(window.localStorage.getItem('menuData')) : [],
    msg: {},
    title:[],
};

const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token;
    },
    SET_INTRODUCTION: (state, introduction) => {
        state.introduction = introduction;
    },
    SET_USERNAME: (state, userName) => {
        state.userName = userName;
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar;
    },
    SET_ROLES: (state, roles) => {
        state.roles = roles;
    },
    SET_MENU: (state, menu) => {
        state.menu = menu;
    },
    SET_USERINFO: (state, userinfo) => {
        state.userinfo = userinfo;
    },
    SET_ISLOGIN: (state, isLogin) => {
        window.localStorage.setItem('isLogin', JSON.stringify(isLogin));
    },
    SET_ISCLOSE: (state, isClose) => {
        state.isClose = isClose;
        window.localStorage.setItem('isClose', JSON.stringify(isClose));
    },
    SET_MENUBAR: (state, menuData) => {
        state.menuData = menuData;
        window.localStorage.setItem('menuData', JSON.stringify(menuData));
    },
    SET_MSG: (state, msg) => {
        state.msg = msg;
    },
    SET_TITLE: (state, title) => {
        state.title = title;
    },
};

const actions = {
    login({ commit, dispatch }, userInfo) {
        const { username, password } = userInfo;
        return new Promise((resolve, reject) => {
            import(pageURL + 'request/Security/Login.js').then(loginapi => {
                let domain = userInfo.domain;
                if (domain == null || domain.length == 0) domain = 'vue';
                if (isFile) {
                    domain = 'client';
                };
                loginapi.login({ userName: username, password: password, domain: domain }).then(async (res) => {
                    commit('SET_ISLOGIN', 1);//登录时设置为1
                    commit('SET_ISCLOSE', 1);//登录时设置为1
                    await dispatch('getmenu');
                    resolve();
                }).catch(() => {
                    reject();
                });
            });
        });
    },
    getmenu({ commit, state }) {
        return new Promise((resolve, reject) => {
            // 访问5000地址上的文件以及请求接口之前需要先人员授权信息加载
            var sAuthBearer = sessionStorage.getItem('token');
            document.cookie = 'X-NXDO-Bearer=' + sAuthBearer + ';path = /';
            axios.get(menuUrl).then(res => {
                commit('SET_MENUBAR', res.data);
                if (isFile) {
                    var sMenuJson = JSON.stringify(res.data);
                    EdgeHost.ChangeMenus(sMenuJson);
                };
                resolve();
            }).catch(() => {
                reject();
            });
        });
    },
    getInfo({ commit, state }) {
        return new Promise((resolve, reject) => {
            var data = {
                roles: ['admin'],
                introduction: 'I am a super administrator',
                avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
                userName: 'Admin',
                menu: routeData.routes,
                userinfo: info.data
            };
            commit('SET_USERINFO', info.data);
            commit('SET_ROLES', data.roles);
            commit('SET_INTRODUCTION', data.introduction);
            commit('SET_AVATAR', data.avatar);
            commit('SET_USERNAME', data.userName);
            commit('SET_MENU', data.menu);
            resolve(data);
            // getMenu().then(res => {
            //     if (!res.data) {
            //         reject('验证失败，请重新登录');
            //     };
            //     getInfo().then(info => {
            //         if (info.code !== 20000 || !info.data) {
            //             reject('获取用户信息失败');
            //         };
            //         var data = {
            //             roles: ['admin'],
            //             introduction: 'I am a super administrator',
            //             avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
            //             userName: 'Admin',
            //             menu: _(res.data).map(e => e.PATH).valueOf(),
            //             userinfo: info.data
            //         };
            //         commit('SET_USERINFO', info.data);
            //         commit('SET_ROLES', data.roles);
            //         commit('SET_INTRODUCTION', data.introduction);
            //         commit('SET_AVATAR', data.avatar);
            //         commit('SET_USERNAME', data.userName);
            //         commit('SET_MENU', data.menu);
            //         resolve(data);
            //     });
            // }).catch(error => {
            //     reject(error);
            // });
        });
    },
    logout({ commit, state, dispatch }) {
        return new Promise((resolve, reject) => {
            // logout().then((res) => {
            //     if (res.code !== 20000) {
            //         reject(res.message);
            //         return;
            //     };
            //     commit('SET_TOKEN', '');
            //     commit('SET_ROLES', []);
            //     removeToken();
            //     resetRouter();
            //     dispatch('tagsView/delAllViews', null, { root: true });
            //     resolve();
            //     router.push('login');
            //     window.location.reload();
            // }).catch(error => {
            //     reject(error);
            // });
            commit('SET_TOKEN', '');
            commit('SET_ROLES', []);
            commit('SET_ISLOGIN', 0);//注销时设置为0
            sessionStorage.removeItem('token');
            // resetRouter();
            dispatch('delAllViews', null, { root: true });
            resolve();
            router.push('/login');
            // window.location.reload();
        });
    },
    resetToken({ commit }) {
        return new Promise(resolve => {
            commit('SET_TOKEN', '');
            commit('SET_ROLES', []);
            removeToken();
            resolve();
        })
    },
    async changeRoles({ commit, dispatch }, role) {
        const token = role + '-token';
        commit('SET_TOKEN', token);
        setToken(token);
        const { roles } = await dispatch('getInfo');
        resetRouter();
        const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true });
        router.addRoutes(accessRoutes);
        dispatch('tagsView/delAllViews', null, { root: true });
    },
    closeSession({ commit, state }, message) {
        commit('SET_ISCLOSE', message);
    },
    setmsg({ commit, state }, message) {
        commit('SET_MSG', message);
    },
    setDocumentTitle({ commit, state }, title) {
        commit('SET_TITLE', title);
    },
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
};
